﻿using DigitalHealth.PACS.Model.Enumeration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace DigitalHealth.PACS.Model
{
    /// <summary>
    /// 消息服务上下文
    /// </summary>
    public class ChatServerContext
    {
        /// <summary>
        /// 聊天类型
        /// </summary>
        public ChatType ChatType { get; set; }
        /// <summary>
        /// 消息内容
        /// </summary>
        public MsgTemplate ChatMessage { get; set; }
    }

    public class ChatServerContextJsonConverter : JsonConverter
    {
        public override bool CanConvert(Type typeToConvert)
        {
            return typeof(ChatServerContext).IsAssignableFrom(typeToConvert);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jsonObject = JObject.Load(reader);
            ChatServerContext result = new ChatServerContext();
            JToken chatType;
            if (jsonObject.TryGetValue("ChatType", out chatType))
            {
                switch (chatType.ToString())
                {
                    case "0":
                        result.ChatMessage = new ChatMessage();
                        break;
                    case "2":
                        result.ChatMessage = new ChatGroupMessage();
                        break;
                    case "3":
                        result.ChatMessage = new NoticeMessage();
                        break;
                    default:
                        break;
                }
            }
            serializer.Populate(jsonObject.CreateReader(), result);
            return result;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
}
